package com.scau.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.scau.dao.PaymentFlowMapper;
import com.scau.dto.CBECReturn;
import com.scau.dto.Order;
import com.scau.handleException.ResultCode;
import com.scau.pojo.*;
import com.scau.service.IEWalletService;
import com.scau.service.IOrderService;
import com.scau.service.ISystemUserService;
import com.scau.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.scau.service.IMenuService;

import java.util.Date;
import java.util.List;


@Controller
@CrossOrigin
@RequestMapping("")
public class AdminController {

    @Autowired
    IMenuService menuService;

    @Autowired
    IOrderService orderService;

    @Autowired
    IEWalletService ieWalletService;

    @Autowired
    PaymentFlowMapper paymentFlowMapper;

    @Autowired
    ISystemUserService systemUserService;

    // 增加菜单
    @RequestMapping(value = "/menu/add",method = RequestMethod.POST,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn addMenu(@RequestBody Menu menu){
        System.out.println(menu);
        menuService.addMenu(menu);
        return CBECReturn.ok();
    }


    // 显示所有菜单
    @RequestMapping(value = "/menu/list",method = RequestMethod.GET, produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn showMenus() throws JsonProcessingException {
        List<Menu> menuList = menuService.showMenus();

        /*String json = null;
        json = JsonUtil.toJson(menuList);*/
        return CBECReturn.ok(menuList);
    }

    // 删除菜单
    @RequestMapping(value = "/menu/delete/{id}",method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn deleteMenu(@PathVariable("id") int id){
        menuService.deleteMenu(id);
        return CBECReturn.ok();
    }

    // 修改菜单
    @RequestMapping(value = "/menu/update",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn updateMenu(@RequestBody Menu menu){
        System.out.println("修改菜单");
        System.out.println(menu);
        menuService.updateMenu(menu);
        return CBECReturn.ok();
    }

//    // 修改用户角色属性
//    @RequestMapping(value = "/admin/user/role/{id}",method = RequestMethod.POST)
//    public String updateUserRole(@PathVariable("id") int id, SystemUser systemUser){
//
//        systemUserService.updateUser(systemUser);
//        return "success";
//    }
//
//
//    // 修改用户菜单属性
//    @RequestMapping(value = "/admin/user/menu/{id}",method = RequestMethod.POST)
//    public String updateUserMenu(@PathVariable("id") int id, SystemUser systemUser){
////        menuService.updateMenu(menu);
//        return "success";
//    }




    // 查看所有订单
    @RequestMapping(value = "/admin/order/list",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn showOrders() throws JsonProcessingException {
        List<OrderItem> orderList = orderService.getOrderItems();

        /*String json = null;
        json = JsonUtil.toJson(orderList);*/
        return CBECReturn.ok(orderList);
    }

    // 修改订单
    @RequestMapping(value = "/admin/order/update",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn updateOrder(@RequestBody OrderItem orderItem){
//        Date date = new Date();
//        orderItem.setCreateDate(date);
        System.out.println("修改订单");
        System.out.println(orderItem);

        orderService.updateOrderItem(orderItem);
        return CBECReturn.ok();
    }


    // 添加订单
    @RequestMapping(value = "/admin/order/add",method = RequestMethod.POST,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn addOrder(@RequestBody OrderItem orderItem){
        Date date = new Date();
        orderItem.setCreateDate(date);
        System.out.println(orderItem);
        orderService.addOrderItem(orderItem);
        return CBECReturn.ok();
    }

    // 删除订单
    @RequestMapping(value = "/admin/order/delete/{id}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn deleteOrder(@PathVariable("id") String id){
        orderService.deleteOrderItem(id);
        return CBECReturn.ok();
    }


    // 显示所有用户
    @RequestMapping(value = "/admin/user/list",method = RequestMethod.GET)
    @ResponseBody
    public CBECReturn showUsers() throws JsonProcessingException {
        List<SystemUser> systemUserList = systemUserService.getUsers();

        String json = null;
        json = JsonUtil.toJson(systemUserList);
        return CBECReturn.ok(systemUserList);
    }

    // 添加用户
    @RequestMapping(value = "/admin/user/add",method = RequestMethod.POST)
    @ResponseBody
    public CBECReturn addUser(SystemUser systemUser){
        systemUserService.addUser(systemUser);
        return CBECReturn.ok();
    }

    // 删除用户
    @RequestMapping(value = "/admin/user/delete/{username}", method = RequestMethod.DELETE)
    @ResponseBody
    public CBECReturn deleteUser(@PathVariable("username") String username){
        systemUserService.deleteUser(username);
        return CBECReturn.ok();
    }

    // 修改用户
    @RequestMapping(value = "/admin/user/update",method = RequestMethod.PUT)
    @ResponseBody
    public CBECReturn updateUser(SystemUser systemUser){
        System.out.println("修改");
        System.out.println(systemUser);
        systemUserService.updateUser(systemUser);
        return CBECReturn.ok();
    }

    // 显示所有流水


    @RequestMapping(value="/wallet/getPaymentFlow/list",method = RequestMethod.GET,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn ListPaymentFlow(){

//        EWallet wallet = getWalletByUsername(username);
//        String FromId = wallet.getPayId();
        PaymentFlowExample example = new PaymentFlowExample();
        example.createCriteria().andIdIsNotNull();
        List<PaymentFlow> paymentFlows = paymentFlowMapper.selectByExample(example);

        if(paymentFlows != null)
            return CBECReturn.build(ResultCode.SUCCESS,paymentFlows);
        else
            return CBECReturn.build(ResultCode.Failed);
    }
    // 修改流水
    @RequestMapping(value = "/wallet/passPayment/{id}/{flag}",method = RequestMethod.POST,produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn passPayment(@PathVariable("id") String id, @PathVariable("flag") String flag){
        int result = ieWalletService.CashOutOrRechangePass(id, Integer.parseInt(flag));

        if(result == 1)
            return CBECReturn.build(ResultCode.SUCCESS);
        else
            return CBECReturn.build(ResultCode.Failed);
    }


}
